package com.xwj.back.controller.user;


import com.alibaba.fastjson2.JSONObject;
import com.xwj.back.pojo.User;
import com.xwj.back.service.user.UserService;
import com.xwj.back.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class UserController {


    @Autowired
    private UserService userService;

    @GetMapping("/user/get/")
    public JSONObject getAllUser(@RequestParam("role") String role,@RequestParam("page") Integer page){
        return userService.getAllUser(role,page);
    }
    @GetMapping("/user/get/all/")
    public List<User> getUsers(){
        return userService.getUsers();
    }

    @GetMapping("/user/update/status/")
    public Map<String,String> updateUserStatus(@RequestParam Map<String,String> data){
        return userService.updateUserStatus(data);
    }

    @PostMapping("/user/add/")
    public Map<String,String> addUser(@RequestBody Map<String,String> data){
        return userService.addUser(data);
    }

    @PostMapping("/user/update/")
    public Map<String,String> updateUser(@RequestBody Map<String,String> data){
        return userService.updateUser(data);
    }

    @PostMapping("/user/delete/")
    public Map<String,String> deleteUser(@RequestBody Map<String,String> data){
        return userService.deleteUser(data);
    }
    //上传头像
    @PostMapping("/user/upload/avatar/")
    public Map<String,String> uploadAvatarImg(MultipartFile file) throws IOException {
        String upload = FileUploadUtil.upload(file);
        Map<String, String> map = new HashMap<>();
        map.put("avatarUrl",upload);
        return map;
    }
    //根据角色搜索
    @GetMapping("/user/get/byRole/")
    public JSONObject getUsersByRole(@RequestParam("role") String role,@RequestParam("operateRole") String operateRole){
        return userService.getUsersByRole(role,operateRole);
    }
    //根据名称模糊查询
    @GetMapping("/user/get/byName/")
    public JSONObject getUsersByName(@RequestParam("role") String role,@RequestParam("username") String username){
        return userService.getUsersByName(role,username);
    }
    //批量删除
    @PostMapping("/user/bulkDelete/")
    public Map<String,String> bulkDelete(@RequestBody Map<String,String> data){
        Map<String,String> map = new HashMap<>();
        map.put("message",userService.bulkDelete(data));
        return map;
    }
}
